﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

namespace GridPaging
{
    [ToolboxData("<{0}:DropDownListEx runat=server></{0}:DropDownListEx>")]
    public class DropDownListEx : DropDownList
    {
        #region Properties
        /// <summary>
        /// Returns the selected value of the control before the postback occured.
        /// </summary>
        [Browsable(false)]
        public String PreviousValue
        {
            get { return _previousValue; }
        }
        private String _previousValue = String.Empty;

        /// <summary>
        /// Gets or sets a value using viewstate. (String)
        /// </summary>
        /// <value></value>
        [DefaultValue(""), Category("Appearance")]
        [Browsable(true)]
        public String ListId
        {
            get
            {
                object obj = base.ViewState["ListId"];
                return (obj == null) ? "" : (String)obj;
            }
            set { base.ViewState["ListId"] = value; }
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Finds the by value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        protected int FindByValue(string value)
        {
            int num1 = 0;
            foreach (ListItem item1 in this.Items)
            {
                if (item1.Value.Equals(value))
                {
                    return num1;
                }
                num1 += 1;
            }
            return -1;
        }
        #endregion

        #region Base Overrides
        /// <summary>
        /// Processes postback data for the <see cref="T:System.Web.UI.WebControls.DropDownList"></see> control.
        /// </summary>
        /// <param name="postDataKey">The index within the posted collection that references the content to load.</param>
        /// <param name="postCollection">The collection of all incoming name values posted to the server.</param>
        /// <returns>
        /// true if the <see cref="T:System.Web.UI.WebControls.DropDownList"></see> control's state changes as a result of the postback event; otherwise, false.
        /// </returns>
        protected override bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            _previousValue = this.SelectedValue;

            string[] textArray1 = postCollection.GetValues(postDataKey);
            if (textArray1 == null)
            {
                return false;
            }

            int num1 = this.FindByValue(textArray1[0]);
            if (this.SelectedIndex != num1)
            {
                this.SelectedIndex = num1;
                return true;
            }
            return false;
        }
        #endregion
    }
}
